package com.sinya.shoppingmall.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * @author CoCo
 * @date 2016/1/21.
 */
public class MyListView extends ListView{

    public static final int SCALE = 4;

    public MyListView(Context context) {
        super(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    private float mLastY = 0;
    private int mMoveY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //按下点的y坐标
                mLastY = ev.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                //记录在Y轴的移动距离
                mMoveY = (int) (mLastY - ev.getY());
                //将移动过程中得到的y值动态付给down下的坐标，作为下次move的起始坐标
                mLastY = ev.getY();
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                //TODO 为什么置为0？松手后因惯性滑动造成产生的move距离会使控件仍然移动
                mMoveY = 0;
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        //获取到每个item里的ImageView，并通知他们滚动的距离
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            ViewGroup viewGroup = (ViewGroup) getChildAt(i);
            MyImageView imageView = (MyImageView) viewGroup.getChildAt(0);
            imageView.setOnYsrcollBy(mMoveY / SCALE);
        }
    }
}
